package com.sltx.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.MessageFormat;

/**
 * Created with IntelliJ IDEA.
 * User: liuying
 * Date: 16-7-21
 * Time: 上午9:33
 * To change this template use File | Settings | File Templates.
 */
public class TextUtils {
    public final static String format(String pattern, Object... param) {
        return new MessageFormat(pattern.replaceAll("'", "`")).format(param);
    }

    public static  void formatField(Object model) throws NoSuchMethodException,
            IllegalAccessException,IllegalArgumentException,InvocationTargetException{

        Field[] fields = model.getClass().getDeclaredFields();
        for (int i=0;i<fields.length;i++){
            String name= fields[i].getName();
            name = name.substring(0,1).toUpperCase()+name.substring(1);
            String type = fields[i].getGenericType().toString();
            if(type.equals("class java.lang.String")){
                Method method = model.getClass().getMethod("get"+name);
                String value = (String) method.invoke(model);
                if (value==null){
                    value="";
                }
            }
                if(type.equals("class java.lang.Integer")){
                    Method   method = model.getClass().getMethod("get" + name);
                    Integer intvalue = (Integer) method.invoke(model);
                    if (intvalue==null){
                        intvalue=0;
                    }
                }
            if(type.equals("class java.lang.Double")){
                Method   method = model.getClass().getMethod("get" + name);
                Double intvalue = (Double) method.invoke(model);
                if (intvalue==null){
                    intvalue= Double.valueOf(0);
                }
            }

            if(type.equals("class java.math.BigDecimal")){
                Method   method = model.getClass().getMethod("get" + name);
                BigDecimal intvalue = (BigDecimal) method.invoke(model);
                if (intvalue==null){
                    intvalue= BigDecimal.valueOf(0);
                }
            }

        }
    }
}
